/*
 *    Copyright (c) 2018-2025, fanxl All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: fanxl (xunlian_fan001@126.com)
 */

package com.taiji.dicom4cloud.smart.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.taiji.dicom4cloud.common.core.utils.R;
import com.taiji.dicom4cloud.common.log.annotation.SnowLog;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 
 *
 * @author fanxl
 * @date 2021-07-06 10:46:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rfidrecord" )
@Api(value = "rfidrecord", tags = "管理")
public class AlarmRecordController {

    private static final Logger LOG = LoggerFactory.getLogger(AlarmRecordController.class);

    private final AlarmRecordService alarmRecordService;



    /**
     * 分页查询
     * @param page 分页对象
     * @param rfidRecord 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getAlarmRecordPage(Page page, RfidRecord rfidRecord) {
       return R.ok(alarmRecordService.getAlarmRecordPage(page,rfidRecord));
    }


    @ApiOperation(value = "RFID报警记录查询", notes = "报警记录查询")
    @GetMapping("/list" )
    public R getAlarmRecords(@RequestParam("current") Integer current,
                             @RequestParam("total") Integer total,
                             @RequestParam("orgId") String orgId,
                             @RequestParam("type") String type) {
        return R.ok(alarmRecordService.getAlarmRecords(current,total,orgId,type));
    }



    @ApiOperation(value = "RFID接警", notes = "RFID接警")
    @GetMapping("/deal" )
    public R dealAlarm(RfidRecord rfidRecord) {
        RfidRecord rfidRecord1 = alarmRecordService.getById(rfidRecord.getId());
        if(ObjectUtil.isNotNull(rfidRecord1)){
            if(ObjectUtil.isNotNull(rfidRecord.getIsDeal())){
                rfidRecord.setIsDeal("Y");
            }
            if(ObjectUtil.isNotNull(rfidRecord.getIsError())){
                rfidRecord.setIsError(rfidRecord.getIsError());
            }
        }
        return R.ok(alarmRecordService.saveOrUpdate(rfidRecord));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(alarmRecordService.getById(id));
    }

    /**
     * 新增
     * @param rfidRecord 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SnowLog("新增" )
    @PostMapping
    public R save(@RequestBody RfidRecord rfidRecord) {
        return R.ok(alarmRecordService.save(rfidRecord));
    }

    /**
     * 修改
     * @param rfidRecord 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SnowLog("修改" )
    @PutMapping
    public R updateById(@RequestBody RfidRecord rfidRecord) {
        return R.ok(alarmRecordService.updateById(rfidRecord));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SnowLog("通过id删除" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(alarmRecordService.removeById(id));
    }

    @PostMapping("/video")
    public R getVideoUrl(@RequestBody Map<String,String> map){
        return R.ok(alarmRecordService.getVideoUrl(map));
    }


}
